import { WS_ADDRESS } from '../configs'

// import {WS_ADDRESS} from '../configs'

function useWebsocket (handleMessage) {
    const ws = new WebSocket(WS_ADDRESS)

    const init = () =>{
        bindEvent()
    }

    function bindEvent () {
        ws.addEventListener('open', handleOpen, false)
        ws.addEventListener('cloase', handleClose, false)
        ws.addEventListener('error', handleError, false)
        ws.addEventListener('message', handleMessage, false)
    }

    function handleOpen (e) {
        console.log('WebSocket open', e);
    }
    
    function handleClose (e) {
        console.log('WebSocket cloase', e);
    }
    
    function handleError (e) {
        console.log('WebSocket error', e);
    }

    // function handleMessage (e) {
    //     console.log('WebSocket message', e);
    // }

    init()

    return ws
}

export default useWebsocket